package beans;

import dao.DocumentDAO;
import model.Document;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import utils.GetBeanUtil;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

@ManagedBean(name = "documentsBean")
@SessionScoped
public class DocumentsBean {
    private Document document;
    private List<Document> allDocuments;
    private StreamedContent file;

    private List<Document> adminDoc;

    public Document getDocument() {
        return document;
    }

    public void setDocument(Document document) {
        this.document = document;
    }

    public StreamedContent getFile() {
        downloadFile();
        return file;
    }

    public void setFile(StreamedContent file) {
        this.file = file;
    }

    @PostConstruct
    public void init() {
        getAllDocuments();
    }

    public List<Document> getAllDocuments() {
        DocumentDAO documentDAO = new DocumentDAO();
        if(allDocuments == null) {
            allDocuments = documentDAO.getDocumentsByPermission(0);
        }
        if(allDocuments.size() != documentDAO.getCountOfDocuments()) {
            allDocuments = documentDAO.getDocumentsByPermission(0);
        }
        return allDocuments;
    }

    public void setAllDocuments(List<Document> allDocuments) {
        this.allDocuments = allDocuments;
    }

    public List<Document> getAdminDoc() {
        DocumentDAO documentDAO = new DocumentDAO();
        if(adminDoc == null) {
            adminDoc = documentDAO.getAllDocuments();
        }
        if(adminDoc.size() != documentDAO.getCountOfDocuments()) {
            adminDoc = documentDAO.getAllDocuments();
        }
        return adminDoc;
    }

    public void setAdminDoc(List<Document> adminDoc) {
        this.adminDoc = adminDoc;
    }

    public void downloadFile() {
        try {
            InputStream inputStream = new FileInputStream("D:\\FILES\\" + document.getIdDocument());
            file = new DefaultStreamedContent(inputStream, "type", document.getDocumentName());
        }
        catch (FileNotFoundException e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", e.toString()));
        }
    }

    public void save() {
        try {
            DocumentDAO documentDAO = new DocumentDAO();
            if(document.getStorageLife() == 0) {
                document.setDeleteDate(null);
            }
            documentDAO.updateDocument(document);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Updated"));
        }
        catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", e.toString()));
        }
    }

    public void delete() {
        try {
            DocumentDAO documentDAO = new DocumentDAO();
            documentDAO.deleteDocumentById(document.getIdDocument());
            new File("D:\\FILES\\" + document.getIdDocument()).delete();
            NavigationBean navigationBean = (NavigationBean) GetBeanUtil.getBean("navigationBean");
            allDocuments = null;
            navigationBean.action("all.xhtml");
        }
        catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR", e.toString()));
        }
    }




}
